var app = getApp()
Page({
    data: {
        // daydata: [ '1天',  '2天', '7天', '15天'],
        selected: true,
        selected1: false,
        datasell: '',
        databuy: '',
        data: '',
        dataTop: '',
        nodata: '',
        maskyq: false,
        maskcall: false,
        overly: false,
        buyid: '',
        sellid: '',
        navtype: '',
        listid: '',
        day: ''
    },
    //关闭弹窗
    close: function(e) {
        this.setData({
            maskyq: false,
            maskcall: false,
            overly: false
        })
    },
    selected: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected1: false,
            selected: true,
            navtype: type
        })
        this.sell()
    },
    selected1: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected: false,
            selected1: true,
            navtype: type
        })
        this.buy()
    },
    // 选择延期天数
    daybtn: function(e) {
        var day = e.currentTarget.dataset.select;
        console.log(day)
        var ids = e.currentTarget.dataset.id; //获取自定义的id   
        this.setData({
            id: ids, //把获取的自定义id赋给当前组件的id(即获取当前组件)  
            day: day
        })

    },
    // 延期弹窗
    maskbuy: function(e) {
        var id = e.target.id
        this.setData({
            maskyq: true,
            overly: true,
            buyid: id
        })
    },
    //取消委托弹窗
    cancel: function(e) {
        var id = e.target.id
        this.setData({
            maskcall: true,
            overly: true,
            listid: id
        })
    },
    //申请延期卖
    delay: function(e) {
        var type = e.target.dataset.type
        var id = e.target.id
        var that = this;

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/mytask/delay',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        delay_num: parseInt(that.data.day),
                        type: that.data.navtype,
                        id: parseInt(that.data.buyid)
                    },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                maskyq: false,
                                overly: false,
                            })
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })

    },
    cancelbtn: function(e) {
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/mytask/cancel',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        type: that.data.navtype,
                        id: parseInt(that.data.listid)
                    },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                maskcall: false,
                                overly: false,
                            })
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })

    },
    buy: function(e) {
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                // 帮买列表
                wx.request({
                    url: app.url.requestUrl + '/mytask/buy',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        page: 1
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                databuy: res.data.data,

                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                image: '../../img/icon-prompt.png',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })
    },
    sell: function(e) {
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                // 帮卖列表
                wx.request({
                    url: app.url.requestUrl + '/mytask/sell',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        page: 1
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                datasell: res.data.data,

                            })
                        } else {
                            that.setData({
                                nodata: '../img/no_data.png'
                            })
                            wx.showToast({
                                title: res.data.msg,
                                image: '../../img/icon-prompt.png',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.sell()
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                // 我的标签
                wx.request({
                        url: app.url.requestUrl + '/mytask/mylabel',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,

                        },
                        success: function(res) {
                            if (res.data.code == 1) {
                                that.setData({
                                    data: res.data.data,
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                    image: '../../img/icon-prompt.png',
                                    duration: 2000,
                                    mask: true
                                })
                            }

                        }
                    }),


                    //头部数据
                    wx.request({
                        url: app.url.requestUrl + '/mytask/buy_top',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                        },
                        success: function(res) {
                            if (res.data.code == 1) {
                                res.data.data.score = app.globalData.levelRuleCount(res.data.data.score)
                                that.setData({
                                    dataTop: res.data.data,
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                    image: '../../img/icon-prompt.png',
                                    duration: 2000,
                                    mask: true
                                })
                            }

                        }
                    })


            }
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

})